<template>
    <header style="padding-top: 3rem;">
        <img alt="Vue logo" class="logo" src="@/assets/haojiutao.jpg" width="150" height="150" />
        <div class="wrapper">
            <HelloWorld msg="欢 迎 登 录" />
        </div>
    </header>
    <van-form @submit="toLogin">
        <van-cell-group>
            <van-field v-model="loginObj.phoneNum" left-icon="smile-o" placeholder="请输入手机号" size="large" />
            <van-field v-model="loginObj.userPwd" clearable left-icon="music-o" placeholder="请输入密码" size="large"
                :rules="[{ required: true, message: '请输入密码!' }]" type="userPwd" />
            <van-field v-model="loginObj.verifyCode" clearable maxlength="4" placeholder="输入验证码"
                :rules="[{ required: true, message: '请输入验证码' }]">
                <template #button>
                    <van-image width="100" height="40" style="margin-left: 10px;" :src="kaptchaSrc" @click="flushKaptch" />
                </template>
            </van-field>
            <van-button type="primary" block round class="login_btn" native-type="submit">登 &nbsp; &nbsp; &nbsp;
                录</van-button>
            <div style="text-align: right; margin: 10px">
                <a href="#" @click="toRegister">立即注册？</a>
            </div>
        </van-cell-group>
    </van-form>
</template>
  
<script setup>
//导入 HelloWorld 组件，因为 “欢迎登录” 使用的 Helloword 组件
import HelloWorld from '../components/HelloWorld.vue'
//   import kaptcha from '@/assets/kaptcha.jpg'
import { ref, reactive, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router'
import http from '@/utils/request.js'; //{default as htpp}
import { showToast, showSuccessToast, showFailToast, showNotify } from 'vant';
// import {  POST } from './../utils/request.js';
const router = useRouter(); //获取路由器 router 对象
let loginObj = reactive({
    phoneNum: '',
    verifyCode: '',
    userPwd: '',
    uuid: ''
});

//获取验证码
let kaptchaSrc = ref("");
//情况一：页面加载之后获取图片验证码，情况二：用户点击图片验证码刷新
function flushKaptch() {
    console.log('flushKaptcha .....')
    //请求获取图片验证码接口
    // axios.get("http://localhost:8080/user/kaptcha?t=" + Date.now(), null)
    http.get("/user/kaptcha?t=" + Date.now())
        .then(function (response) {
            // 处理成功情况
            kaptchaSrc.value = "data:image/jpg;base64," + response.data.data.img;
            loginObj.uuid = response.data.data.uuid;  //保存uuid
            console.log('flushKaptcha .....成功返回：', response.data.data.img);
        })
        .catch(function (error) {
            // 处理错误情况
            console.log('flushKaptcha .....异常返回：', error);
        });
}


//用户登录请求
const toLogin = function () {
    console.log('toLogin......', loginObj);
    http.post("/user/login", loginObj)
        .then(function (response) {
            // 处理成功情况
            console.log('login .....成功返回：', response.data);
            const { code, message } = response.data;
            if (200 === code) {
                showNotify({ type: 'success', message: '登录成功' });
                 //将 token 保存至前端
                 localStorage.setItem("token",response.data.data.token);
                console.log(localStorage.getItem("token"));
                setTimeout(() => {
                    router.push("/");  //使用路径 path 导航
                }, 1500);
            } else {
                showNotify({ type: 'danger', message });
            }
        })
        .catch(function (error) {
            // 处理错误情况
            console.log('register .....异常返回：', error);
        });
}

//跳转至注册页面
const toRegister = function () {
    //TODO 需要实现跳转功能
    router.push('/regist');
}

onMounted(() => {
    console.log('钩子函数 onMounted 已执行......')
    flushKaptch();//页面DOM对象加载后获取验证码
})
</script>
  
<style scoped>
.login_title {
    text-align: center;
    margin: 50px 0;
    height: 50px;
    font-size: 35px;
}

.login_btn {
    margin-top: 30px;
    font-size: 20px;
}

header {
    line-height: 1.5;
    max-height: 100vh;
}

.logo {
    display: block;
    margin: 0 auto 2rem;
}

nav {
    width: 100%;
    font-size: 12px;
    text-align: center;
    margin-top: 2rem;
}

nav a.router-link-exact-active {
    color: var(--color-text);
}

nav a.router-link-exact-active:hover {
    background-color: transparent;
}

nav a {
    display: inline-block;
    padding: 0 1rem;
    border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
    border: 0;
}

@media (min-width: 1024px) {
    header {
        display: flex;
        place-items: center;
        padding-right: calc(var(--section-gap) / 2);
    }

    .logo {
        margin: 0 2rem 0 0;
    }

    header .wrapper {
        display: flex;
        place-items: flex-start;
        flex-wrap: wrap;
    }

    nav {
        text-align: left;
        margin-left: -1rem;
        font-size: 1rem;

        padding: 1rem 0;
        margin-top: 1rem;
    }
}
</style>